﻿/*--利益公式解析及计算方法（变量及引擎的提取及计算）*/
/*
 * getIndexs 查找strFindValue在内容字符串中的位置数组
 * strContex: 内容字符串
 * strFindValue：查找字符串
 */
function getIndexs(strContex, strFindValue) {
    var indexs = new Array();
    var currentIndex = strContex.indexOf(strFindValue);
    // 找到索引后将索引值记录到数组中
    if (currentIndex != -1) {
        indexs.push(currentIndex);
    }

    while (currentIndex + 1 < strContex.length && currentIndex != -1) {
        nextIndex = strContex.indexOf(strFindValue, currentIndex + 1);
        if (nextIndex != -1) {
            indexs.push(nextIndex);
        }
        currentIndex = nextIndex;
    }
    return indexs;
}

/*
 * getBenefitExpressionParam
 * 获取易保通利益表达式中的普通变量名称
 *
 */
function getBenefitExpressionParam(expression) {
    var startIndexs = getIndexs(expression, "<");
    var endIndexs = getIndexs(expression, ">");
    if (startIndexs.length != endIndexs.length) {
        writeLog(new Error("利益表达式的变量格式不正确"), "<>的数量不匹配");
        return null;
    }
    var params = new Array();
    for (var i = 0; i < startIndexs.length; i++) {
        var paramName = String(expression).substring(startIndexs[i] + 1, endIndexs[i]);
        if ($.inArray(paramName, params) < 0) {
            params.push(paramName);
        }
    }
    return params;
}
/*
 * getBenefitExpressionBenefitParam
 * 获取易保通利益表达式中的利益变量名称
 *
 */
function getBenefitExpressionBenefitParam(expression) {
    var splitIndexs = getIndexs(expression, "$");
    if (splitIndexs.length % 2) {
        writeLog(new Error("利益表达式的利益名称格式不正确"), "$的数量不匹配");
        return null;
    }
    var benefitNames = new Array();
    for (var i = 0; i < splitIndexs.length; i = i + 2) {
        var name = String(expression).substring(splitIndexs[i] + 1, splitIndexs[i + 1]);
        if ($.inArray(name, benefitNames) < 0) {
            benefitNames.push(name);
        }
    }
    return benefitNames;
}
/*
 * getBenefitExpressionEngine
 * 获取易保通利益表达式中的引擎描述
 *
 */
function getBenefitExpressionEngine(expression) {
    var splitIndexs = getIndexs(expression, "!");
    if (splitIndexs.length % 2) {
        writeLog(new Error("利益表达式的引擎格式不正确"), "！的数量不匹配");
        return null;
    }
    var engines = new Array();
    for (var i = 0; i < splitIndexs.length; i = i + 2) {
        var paramName = String(expression).substring(splitIndexs[i] + 1, splitIndexs[i + 1]);
        if ($.inArray(paramName, engines) < 0) {
            engines.push(paramName);
        }
    }
    return engines;
}
/*
 * getBenefitExpressionEngine
 * 获取易保通利益表达式中的引擎描述
 *
 */
function getBenefitExpressionEngineBysplitMark(expression, splitmark) {
    var splitIndexs = getIndexs(expression, splitmark);

    if (splitIndexs.length % 2) {
        writeLog(new Error("利益表达式的引擎格式不正确"), splitmark + "的数量不匹配");
        return null;
    }
    var engines = new Array();
    for (var i = 0; i < splitIndexs.length; i = i + 2) {
        var paramName = String(expression).substring(splitIndexs[i] + 2, splitIndexs[i + 1]);
        if ($.inArray(paramName, engines) < 0) {
            engines.push(paramName);
        }
    }
    return engines;
}
// 替换公式中的引擎
function ReplaceExpEngine(exp) {
    var expression = exp;
    // 替换利益算式中的引擎部分(嵌套引擎)
    for (var fl = 1; fl <= 9; fl++) {
        var splitMart = "F" + fl;
        var engines = getBenefitExpressionEngineBysplitMark(expression, splitMart);
        
        for (var engIndex = 0; engIndex < engines.length; engIndex++) {
            if ($.isArray(eval(engines[engIndex]))) {
                expression = expression.replaceAll(splitMart + engines[engIndex] + splitMart, engines[engIndex]);
            } else {
                expression = expression.replaceAll(splitMart + engines[engIndex] + splitMart, EngineAnalyze(engines[engIndex]));
            }
        }
    }
    // 替换利益算式中的引擎部分
    var engines = getBenefitExpressionEngine(expression);
    for (var engIndex = 0; engIndex < engines.length; engIndex++) {
        expression = expression.replaceAll("!" + engines[engIndex] + "!", String(EngineAnalyze(engines[engIndex])));
    }
    return expression;
}

/*
 * EngineAnalyze
 * 解析引擎
 *
 */
function EngineAnalyze(expression) {
    try {
        // 通过eval方法实现执行引擎方法
        var result = eval(expression);
        return result;
    } catch (e) {
        writeLog(new Error("解析引擎" + expression + "时出错"));
    }
}